﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ras2ClassLib;

namespace Ras2Form
{
    public partial class HitListView : UserControl
    {
        public HitListView()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                HitListController controller = new HitListController();
                foreach (HitList s in controller.GetAllHitLists())
                {
                    m_lvHitLists.Items.Add(CreateListViewItem(s));
                }
            }
        }

        private ListViewItem CreateListViewItem(HitList row)
        {
            ListViewItem item = new ListViewItem(row.Name);

            item.SubItems.Add(row.Weeklist ? "Já" : "Nei");

            item.ImageIndex = 0;
            item.Tag = row;

            return item;
        }

        public void OnNewHitList()
        {
            using (HitListDlg dlg = new HitListDlg())
            {
                dlg.WeekList = true;
                dlg.NewList = true;
                dlg.HitList = new HitList();
                dlg.ShowDialog();
//                 {
//                     HitList hitlist = dlg.HitList;
//                     HitListController controller = new HitListController();
// 
//                     controller.AddHitList(hitlist);
//                     m_lvHitLists.Items.Add(CreateListViewItem(hitlist));
//                 }
            }
        }

        public void OnEditHitList()
        {
            if (m_lvHitLists.SelectedItems.Count == 1)
            {
                ListViewItem listViewItem = m_lvHitLists.SelectedItems[0];

                HitList hitlist = (HitList)listViewItem.Tag;
                using (HitListDlg dlg = new HitListDlg())
                {
                    dlg.HitList = hitlist;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        hitlist = dlg.HitList;
                        HitListController controller = new HitListController();

                        controller.UpdateHitList(hitlist);
                        int nIndex = listViewItem.Index;

                        m_lvHitLists.Items.Remove(listViewItem);
                        m_lvHitLists.Items.Insert(nIndex, CreateListViewItem(hitlist));
                    }
                }
            }            
        }

        public void OnDeleteHitList()
        {
            if (m_lvHitLists.SelectedItems.Count == 1)
            {
                if (MessageBox.Show("Ertu viss um að þú viljir eyða út þessum vinsældarlista?", "Eyða vinsældarlista", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    ListViewItem listViewItem = m_lvHitLists.SelectedItems[0];

                    HitList hitlist = (HitList)listViewItem.Tag;
                    HitListController controller = new HitListController();

                    controller.DeleteHitList(hitlist);
                    m_lvHitLists.Items.Remove(listViewItem);
                }
            }
        }				

        private void OnDoubleClick(object sender, MouseEventArgs e)
        {
            OnEditHitList();
        }
    }
}
